package com.gc.server.controller;

import java.util.List;

import com.gc.server.entity.ImgOrderItemEntity;
import com.gc.server.vo.ImgOrderVo;
import com.yf.request.SearchParam;
import com.yf.utils.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yf.annotation.SysLog;
import java.util.Map;
import com.yf.consts.DaoConst;
import com.gc.server.entity.ImgOrderEntity;
import com.gc.server.service.ImgOrderService;
import com.yf.response.R;

import javax.servlet.http.HttpServletResponse;


/**
 * 线下报单Controller
 *
 * @author hxf
 * @email 505860922@qq.com
 * @date Thu Mar 12 15:17:51 CST 2020
 */
@RestController
@RequestMapping("/img/order")
public class ImgOrderController {
    @Autowired
    private ImgOrderService imgOrderService;

    /**
     * 列表线下报单
     */
    @RequestMapping("/list")
    @RequiresPermissions("img:order:list")
    public R list(@RequestBody SearchParam<ImgOrderVo> params) {
        PageHelper.startPage(params.getPage(),params.getLimit()).setOrderBy(params.getOrder());
        List<ImgOrderVo> imgOrderList = imgOrderService.queryListVo(params.getEntity());
        return R.ok(new PageInfo(imgOrderList));
    }


    /**
     * 列表线下报单下载
     */
    @RequestMapping("/down")
    @RequiresPermissions("img:order:list")
    public R down(@RequestBody SearchParam<ImgOrderVo> params, HttpServletResponse response) {
//        PageHelper.startPage(params.getPage(),params.getLimit()).setOrderBy(params.getOrder());
        List<ImgOrderVo> imgOrderList = imgOrderService.queryListVo(params.getEntity());
        imgOrderService.downLoad(response,imgOrderList);
        return R.ok(new PageInfo(imgOrderList));
    }

    /**
     * 查看线下报单
     */
    @SysLog("查看线下报单")
    @RequestMapping("/info/{id}")
    @RequiresPermissions("img:order:info")
    public R info(@PathVariable("id") Long id) {
        ImgOrderVo orderVo = imgOrderService.getVo(id);
        List<ImgOrderItemEntity> items = imgOrderService.findItems(id);
        orderVo.getImgOrder().setItems(items);
        return R.ok(orderVo);
    }

    /**
     * 保存线下报单
     */
    @SysLog("保存线下报单")
    @RequestMapping("/save")
    @RequiresPermissions("img:order:save")
    public R save(ImgOrderEntity imgOrder) {
        imgOrderService.save(imgOrder);

        return R.ok(imgOrder);
    }

    /**
     * 修改线下报单
     */
    @SysLog("修改线下报单")
    @RequestMapping("/update")
    @RequiresPermissions("img:order:update")
    public R update(@RequestBody ImgOrderEntity imgOrder) {
        imgOrderService.check(imgOrder);
        imgOrder.setAdminName(ShiroUtils.getUserEntity().getUsername());
        imgOrderService.update(imgOrder);
        return R.ok(imgOrder);
    }





    /**
     * 删除线下报单
     */
    @SysLog("删除线下报单")
    @RequestMapping("/delete")
    @RequiresPermissions("img:order:delete")
    public R delete(Long[]ids) {
        imgOrderService.deleteBatch(ids);

        return R.ok();
    }

    /**
     * 所有线下报单
     */
    @SysLog("所有线下报单")
    @RequestMapping("/queryAll")
    public R queryAll (@RequestParam Map<String,Object> params) {

        List<ImgOrderEntity> imgOrderList = imgOrderService.queryList(params);

        return R.ok(imgOrderList);
    }

    /**
    @ModelAttribute
    public SearchParam<ImgOrderEntity> searchParam(SearchParam<ImgOrderEntity> params, ImgOrderEntity imgOrder) {
        if (imgOrder != null) {
            params.setEntity(imgOrder);
        }

        return params;
    }
 **/
    /***********自定义开始***********/
}
